// AWTTextFieldPeer.cpp : implementation file
//

#include "stdafx.h"
//#include "AWTTest.h"
#include "AWTTextFieldPeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTTextFieldPeer, CEdit)

/////////////////////////////////////////////////////////////////////////////
// AWTTextFieldPeer

AWTTextFieldPeer::AWTTextFieldPeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;		
}

AWTTextFieldPeer::~AWTTextFieldPeer()
{
//	if (m_hWnd != NULL)
//		DestroyWindow();
	
	if (m_hWnd != NULL)
	{
		HWND hwnd = m_hWnd;
		::DestroyWindow(hwnd);
		Detach();
	}
}


BEGIN_MESSAGE_MAP(AWTTextFieldPeer, CEdit)
	//{{AFX_MSG_MAP(AWTTextFieldPeer)
	ON_WM_KEYDOWN()
	ON_WM_CHAR()
	ON_WM_KILLFOCUS()
	ON_WM_SETFOCUS()
	ON_WM_KEYUP()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_MESSAGE(WM_AWTSETTEXT, OnAWTSetText)
	ON_WM_CTLCOLOR_REFLECT()
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_WM_GETDLGCODE()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//AWTTextComponetPeer interface 
void AWTTextFieldPeer::getText(CString& text)
{
	GetWindowText(text);
}

void AWTTextFieldPeer::setText(CString& text)
{
	TRACE(_T("AWTTextFieldPeer::setText: %s\n"), text.GetBuffer(0));
	SetWindowText(text.GetBuffer(0));
}

void AWTTextFieldPeer::setEditable(BOOL b)
{
	SetReadOnly(!b);
}

int  AWTTextFieldPeer::getSelectionStart()
{
	int start, end;
	GetSel(start, end);
	return start;
}

int  AWTTextFieldPeer::getSelectionEnd()
{
	int start, end;
	GetSel(start, end);
	return end;
}

void AWTTextFieldPeer::select(int start, int end)
{
	SetSel(start, end);
}

void AWTTextFieldPeer::setCaretPosition(int pos)
{
	CPoint pt = PosFromChar(pos);
	SetCaretPos(pt);
}

int  AWTTextFieldPeer::getCaretPosition()
{
	CPoint pt = GetCaretPos();
  return CharFromPos(pt);
}

/////////////////////////////////////////////////////////////////////////////
// AWTTextFieldPeer message handlers

void AWTTextFieldPeer::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_PRESSED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (!e.consumed())
	{
		if (m_AWTWindow != NULL)
			m_AWTWindow->SendMessage(WM_AWTKEYEVENT, 0, (LPARAM)&e);

		if (IsWindow(m_hWnd))
			CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
	}
}

void AWTTextFieldPeer::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_TYPED, (long)t, nFlags, nChar, nChar);
	m_comp->processKeyEvent(&e);
	if (nChar == VK_TAB)
		return;
	if (nChar == VK_RETURN)
		return;
	CEdit::OnChar(nChar, nRepCnt, nFlags);
}

void AWTTextFieldPeer::OnKillFocus(CWnd* pNewWnd) 
{
	CEdit::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTTextFieldPeer::OnSetFocus(CWnd* pOldWnd) 
{
	CEdit::OnSetFocus(pOldWnd);

	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTTextFieldPeer::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	time_t t=0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTKeyEvent e(m_comp, AWTKeyEvent::KEY_RELEASED, (long)t, nFlags, 0, nChar);
	m_comp->processKeyEvent(&e);
	CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
}

int AWTTextFieldPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CEdit::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	TCHAR szClass[128];
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		::GetClassName(wnd->m_hWnd, szClass, 128);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
		else if (CString(szClass) == CString("#32770"))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/
	return 0;
}

void AWTTextFieldPeer::OnDestroy() 
{
	CEdit::OnDestroy();
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}

long AWTTextFieldPeer::OnAWTSetText(WPARAM wp, LPARAM lp) 
{
	CString* pstr = (CString*)lp;
	SetWindowText(*pstr);
	delete pstr;	
	return 0;
}

HBRUSH AWTTextFieldPeer::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	AWTColor* fg = m_comp->getForeground();
	AWTColor* bk = m_comp->getBackground();
	CBrush* br = m_comp->getBkBrush();

	if (fg != NULL)
		pDC->SetTextColor(fg->getColorRef());
	if (bk != NULL)
		pDC->SetBkColor(bk->getColorRef());
	return (HBRUSH)*br;
}

void AWTTextFieldPeer::OnChange() 
{
	AWTTextEvent e(m_comp, AWTTextEvent::TEXT_VALUE_CHANGED);
	((AWTTextComponent*)m_comp)->processTextEvent(&e);
}

UINT AWTTextFieldPeer::OnGetDlgCode() 
{
	return DLGC_WANTALLKEYS;	
//	return CEdit::OnGetDlgCode();
}

void AWTTextFieldPeer::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
		point.x, point.y, 2, FALSE);
	m_comp->processMouseEvent(&e);
	if (!e.consumed())
		CEdit::OnLButtonDblClk(nFlags, point);
}

void AWTTextFieldPeer::OnLButtonDown(UINT nFlags, CPoint point) 
{
	time_t t = 0;
#ifndef UNDER_CE
	time(&t);
#endif
	AWTMouseEvent e(m_comp, AWTMouseEvent::MOUSE_CLICKED, (long)t, 0, 
		point.x, point.y, 1, FALSE);
	m_comp->processMouseEvent(&e);
	if (!e.consumed())
		CEdit::OnLButtonDown(nFlags, point);
}
